package org.i1510.gen.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.velocity.VelocityContext;
import org.i1510.gen.bean.ConfigBean;
import org.i1510.gen.po.ColumnInfo;
import org.i1510.gen.po.TableInfo;

/**
 * 代码生成器 工具类
 * 
 * @author ruoyi
 */
public class GenUtils {
	/** 项目空间路径 */
	private static final String projectPath = "main/java/org/i1510";

	/** ibatis空间路径 */
	private static final String batisPath = "main/resources/config";

	/** html空间路径 */
	private static final String templatesPath = "main/webapp";

	/**
	 * 设置列信息
	 */
	public static List<ColumnInfo> transColums(List<ColumnInfo> columns) {
		// 列信息
		List<ColumnInfo> columsList = new ArrayList<>();
		for (ColumnInfo column : columns) {
			// 列名转换成Java属性名
			String attrName = GenStringUtils.convertToCamelCase(column
					.getColumnName());
			column.setAttrName(attrName);
			column.setAttrname(GenStringUtils.uncapitalize(attrName));

			// 列的数据类型，转换成Java类型
			String attrType = CommonMap.javaTypeMap.get(column.getDataType());
			column.setAttrType(attrType);

			columsList.add(column);
		}
		return columsList;
	}

	/**
	 * 获取模板信息
	 * 
	 * @return 模板列表
	 */
	public static VelocityContext getVelocityContext(TableInfo table,ConfigBean configBean) {
		// java对象数据传递到模板文件vm
		VelocityContext velocityContext = new VelocityContext();
//		String packageName = "org.i1510.test";
		velocityContext.put("tableName", table.getTableName());
		velocityContext.put("tableComment",
				replaceKeyword(table.getTableComment()));
		velocityContext.put("primaryKey", table.getPrimaryKey());
		velocityContext.put("className", table.getClassName());
		velocityContext.put("classname", table.getClassname());
		velocityContext.put("moduleName", GenUtils.getModuleName(configBean.getPackageName()));//test
		velocityContext.put("columns", table.getColumns());
		velocityContext
				.put("package", configBean.getPackageName());
		velocityContext.put("author", configBean.getAuthor());
		velocityContext.put("datetime", GenDateUtils.getDate());
		velocityContext.put("version", configBean.getVersion());
		return velocityContext;
	}

	/**
	 * 获取模板信息
	 * 
	 * @return 模板列表
	 */
	public static List<String> getTemplates() {
		List<String> templates = new ArrayList<String>();

		templates.add("templates/demo/java/DemoController.java.vm");
		templates.add("templates/demo/java/DemoDao.java.vm");
		templates.add("templates/demo/java/DemoService.java.vm");
		templates.add("templates/demo/java/DemoServiceImpl.java.vm");
		templates.add("templates/demo/java/DemoPo.java.vm");
		templates.add("templates/demo/config/sqlmap-mysql-demo.xml.vm");
		templates.add("templates/demo/html/demo.js.ext.vm");
		templates.add("templates/demo/html/demo.jsp.ext.vm");
		return templates;
	}

	/**
	 * 表名转换成Java类名
	 */
	public static String tableToJava(String tableName) {
//		if (Constants.AUTO_REOMVE_PRE.equals("true")) {
//			tableName = tableName.substring(tableName.indexOf("_") + 1);
//		}
//		if (GenStringUtils.isNotEmpty("sys_")) {
//			tableName = tableName.replace("sys_", "");
//		}
		return GenStringUtils.convertToCamelCase(tableName);
	}

	/**
	 * 获取文件名
	 */
	public static String getFileName(String template, TableInfo table,
			String moduleName) {
		// 小写类名
		String classname = table.getClassname();
		// 大写类名
		String className = table.getClassName();
		String javaPath = projectPath + "/" + moduleName + "/";
		String iBatisPath = batisPath + "/" + moduleName;
		String htmlPath = templatesPath + "/" + moduleName + "/" + classname;
		
		//demo
		if (template.contains("DemoService.java.vm")) {
			return javaPath + "service" + "/" + "I" + className
					+ "Service.java";
		}

		if (template.contains("DemoServiceImpl.java.vm")) {
			return javaPath + "service/impl" + "/" + className
					+ "ServiceImpl.java";
		}

		if (template.contains("DemoDao.java.vm")) {
			return javaPath + "dao" + "/I" + className
					+ "Dao.java";
		}

		if (template.contains("DemoController.java.vm")) {
			return javaPath + "controller" + "/" + className
					+ "Controller.java";
		}

		if (template.contains("DemoPo.java.vm")) {
			return javaPath + "po" + "/" + className
					+ ".java";
		}
		
		if (template.contains("sqlmap-mysql-demo.xml.vm")) {
			return iBatisPath + "/" + "sqlmap-mysql-"+classname+".xml";
		}
		
		if (template.contains("demo.js.ext.vm")) {
			return htmlPath + "/" + "list.js";
		}
		
		if (template.contains("demo.jsp.ext.vm")) {
			return htmlPath + "/" + "list.jsp";
		}
		return null;
	}

	/**
	 * 获取模块名
	 * 
	 * @param packageName
	 *            包名
	 * @return 模块名
	 */
	public static String getModuleName(String packageName) {
		int lastIndex = packageName.lastIndexOf(".");
		int nameLength = packageName.length();
		String moduleName = GenStringUtils.substring(packageName,
				lastIndex + 1, nameLength);
		return moduleName;
	}

	public static String replaceKeyword(String keyword) {
		String keyName = keyword.replaceAll("(?:表|信息)", "");
		return keyName;
	}

	public static void main(String[] args) {
		System.out.println(GenStringUtils.convertToCamelCase("user_name"));
		System.out.println(replaceKeyword("岗位信息表"));
		System.out.println(getModuleName("com.ruoyi.project.system"));
	}
}
